<template> 
    <div class="login_container">
        <div class="login_box">
            <div class="logo">
                <img src="../assets/logo.png" alt="">
            </div>
            <el-form ref="userRef" :rules="userRules" :model="userForm" label-width="1px" class="form_style">
                <el-form-item prop="name">
                    <el-input v-model="userForm.name" prefix-icon="el-icon-user" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item prop="pwd">
                    <!-- prefix-icon="el-icon-lock" 图标 el-icon-图标名 -->
                    <el-input show-password v-model="userForm.pwd" prefix-icon="el-icon-lock" placeholder="密码"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="Login">立刻登录</el-button>
                    <el-button @click="restForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    name : 'login',
    data() {
        return {
            userForm: {
                name: 'zs',
                pwd: '123'
            },
            userRules: {
                name:[
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 2, max: 6, message: '长度在2-6', trigger: 'blur' },
                ],
                pwd:[
                    { required: true, message: '请输入密码', trigger: 'blur' },
                ],
            }
        }
    },
    methods: {
        restForm() {
            // console.log(this);
            this.$refs.userRef.resetFields()
        },
        Login () {
            this.$refs.userRef.validate(async valid =>{
                if (!valid) return
                // console.log("登录操作");
                const { data:res } = await this.$axios.post('/api/user/login/',this.$qs.stringify(this.userForm))
                if ( res.status == 200 ) {
                    // 1.登陆成功后，获取到token,保存token，保存到sessionStorage中
                    window.sessionStorage.setItem('token', res.data.token)
                    // 登录成功，
                    this.$msg.success(res.msg)
                    // 跳转登录成功页面
                    this.$router.push("/home/")
                }else{
                    this.$msg.error(res.msg)
                }
            })
        }
    }
}
</script>

<style lang="less" scoped>
.login_container {
    background-color: blueviolet;
    height: 100%;
}

.login_box {
    width: 550px;
    height: 400px;
    border-radius: 5px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

.logo {
    height: 105.3px;
    widows: 13px;
    border: 1px solid #eee;
    padding: 10px;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 10px rgb(190, 190, 190);

    img {
        width: 100%;
        height: 100%;
    }
}

.form_style {
    position: absolute;
    bottom: 0;
    height: 60%;
    width: 100%;
    padding: 0 10%;
    box-sizing: border-box;
}

.btns {
    display: flex;
    justify-content: flex-end;
}
</style>
